//
//  CityController.m
//  HB
//
//  Created by 孙智慧 on 16/1/11.

//  Copyright © 2016年 孙智慧. All rights reserved.

#import "CityController.h"
#import "CityDJCell.h"
#import "ZYMusicTool.h"
#import "CityNetWorkOperation.h"
#import "SameCityDJOperation.h"
#import "MyTableViewCell.h"
#import "DJPageController.h"

@implementation CityController

-(void) choseTerm:(UITapGestureRecognizer *)imgView andId:(id)data
{
    NSInteger i= imgView.view.tag;
    
    NSString *djid=@"";
    
    CityDJCell *cell=(CityDJCell *)data;
    
    if(i==1)
    {
        djid=cell.djID1;
    }
    else if(i==2)
    {
        djid=cell.djID2;
    }
    else if(i==3)
    {
        djid=cell.djID3;
    }
    
    DJPageController *djPage= [self.storyboard instantiateViewControllerWithIdentifier:@"DJPage"];
    
    [djPage setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    
    djPage.djID=djid;
    
    [self.view.window.rootViewController presentViewController:djPage animated:true completion:nil];
}

#pragma 加载幻灯片
-(void) loadSlide
{
    operation= [[CityNetWorkOperation alloc] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"Recommend" andFuncName:@"slide"];
    
    NSDictionary * dictInfo = @{@"url":URL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}

#pragma 同城DJ -----begin
-(void) loadSameCityDj
{
    operation= [[SameCityDJOperation alloc] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"City" andFuncName:@"SameCityDJ"];
    
    NSString *newURL=[NSString stringWithFormat:@"%@?cityName=%@",URL,self.city];
    
    NSDictionary * dictInfo = @{@"url":newURL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:1];
}
#pragma 同城DJ -----end

#pragma 同城热曲 -----begin
-(void) loadSameCityMusic
{
    operation= [[SameCityDJOperation alloc] initWithDelegate:self];
    
    NSString *URL= [FxGlobal getServiceUrl:@"City" andFuncName:@"HotMusic"];
    
    NSString *newURL=[NSString stringWithFormat:@"%@?cityName=%@&pageIndex=%ld&pageSize=%ld",URL,self.city,pageIndex,pageSize];
    
    NSDictionary * dictInfo = @{@"url":newURL};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:2];
}
#pragma 同城热曲 -----end

-(void) opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    if(flag==1)
    {
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        
        if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
        {
            NSArray *arrayData= [dataParse objectForKey:@"data"];
            
            for (NSInteger i=0; i<[arrayData count]; i++)
            {
                NSDictionary *dict= [arrayData objectAtIndex:i];
                
                [cityDjArray addObject:dict];
            }
            
            [self.djTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
        
        
    }
    else if(flag==2)
    {
        //热播单曲
        //[musicArray removeAllObjects];
        
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSDictionary *dataParse = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
        
        if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
        {
            NSInteger totleRowCount= [[dataParse objectForKey:@"totleRowCount"] integerValue];
            
            if(totleRowCount>pageIndex)
            {
                _hasNextPage=YES;
            }
            else
            {
                _hasNextPage= NO;
            }
            
            NSArray *arrayData= [dataParse objectForKey:@"data"];
            
            for (NSInteger i=0; i<[arrayData count]; i++)
            {
                
                NSDictionary *dict= [arrayData objectAtIndex:i];
                
                [musicArray addObject:dict];
            }
            
            [self.cityHotMusicTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
        }
        
    }
}

-(void) opSuccessEx:(id)data opinfo:(NSDictionary *)dictInfo
{
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *dataParse  = [dataStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
    
    if ([[dataParse objectForKey:@"result"] isEqualToString:@"1"])
    {
        NSArray *arrayData= [dataParse objectForKey:@"data"];
    
        [_customScrollView loadData:arrayData];

    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _hasNextPage=NO;
    
    pageIndex=1;
    
    pageSize=10;
    
    cityDjArray =[[NSMutableArray alloc] init];
    
    musicArray = [[NSMutableArray alloc] init];
    
    RegisterNotify(NofifyMusicPlay, @selector(stopNavBtnRoation));
    
    [self loadSameCityDj];
    
    [self loadSameCityMusic];
    
    self.djTableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    
    self.djTableView.delegate=self;
    
    self.djTableView.dataSource=self;
    
    self.cityHotMusicTableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    
    self.cityHotMusicTableView.delegate=self;
    
    self.cityHotMusicTableView.dataSource=self;
    
    [self setTableHeight];
    
    if(IsiPhone6P)
    {
        //ImageView高度为父视图高度
        NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:self.customScrollView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:168.0f];
        
        [self.contentView addConstraint:heightConstraint];
    }
    else if(IsiPhone6)
    {
        //ImageView高度为父视图高度
        NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:self.customScrollView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:152.0f];
        
        [self.contentView addConstraint:heightConstraint];
    }
    else
    {
        //ImageView高度为父视图高度
        NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:self.customScrollView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:130.0f];
        
        [self.contentView addConstraint:heightConstraint];
    }
    
    [_customScrollView layoutUI];
    
    [self loadSlide];
}

-(void) stopNavBtnRoation
{
    [self stopNavRightBtnRoation];
    
    [self.marqueeView stopM];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if([self musicPlay].isRoationing)
    {
        self.marqueeView.musicName= [self musicPlay ].playingMusic.name;
        
        [self.marqueeView startM];
    }
    else
    {
        [self.marqueeView stopM];
    }
    
    self.lblCityName.text= self.city;
}

-(void) setTableHeight
{
    NSLayoutConstraint* heightConstraint =nil;
    
    if(IsiPhone6)
    {
        //ImageView高度为父视图高度
        heightConstraint = [NSLayoutConstraint constraintWithItem:self.djTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:320.0f];
        
    }
    else if(IsiPhone6P)
    {
        //ImageView高度为父视图高度
        heightConstraint = [NSLayoutConstraint constraintWithItem:self.djTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:360.0f];
    }
    else if(IsiPhone5)
    {
        //ImageView高度为父视图高度
        heightConstraint = [NSLayoutConstraint constraintWithItem:self.djTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:280.0f];
    }
    else
    {
        //ImageView高度为父视图高度
        heightConstraint = [NSLayoutConstraint constraintWithItem:self.djTableView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:240.0f];
    }
    
    [self.contentView addConstraint:heightConstraint];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([tableView isEqual:self.djTableView])
    {
        if(IsiPhone6)
        {
            return 160; //图片宽度 116
        }
        else if(IsiPhone6P)
        {
            return 180; // 图片宽度 136
        }
        else if(IsiPhone5)
        {
            return 140; //图片宽度 96
        }
        else
        {
            return 120;
        }
    }
    else
    {
        return 55;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if([tableView isEqual:self.djTableView])
    {
        return cityDjArray.count/3+(cityDjArray.count%3>0?1:0);
    }
    else
    {
        return  _hasNextPage?musicArray.count+1:musicArray.count;
    }
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if([tableView isEqual:self.cityHotMusicTableView])
    {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        MyTableViewCell *cell = (MyTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
        
        if(![[ZYMusicTool playingMusic].filename isEqualToString:[cell.musicInfo objectForKey:@"MusicName"]])
        {
            [ZYMusicTool playingMusic].playing=NO;
            //获取新的歌曲
            ZYMusic *currMusic=[[ZYMusic alloc]init];
            
            currMusic.name= [cell.musicInfo objectForKey:@"MusicName"];
            
            currMusic.icon= [cell.musicInfo objectForKey:@"Icon"];
            
            currMusic.iconName= [cell.musicInfo objectForKey:@"IconName"];
            
            currMusic.musicListArray = [cell.musicInfo objectForKey:@"musiclist"];
            
            currMusic.lrcname=@"";
            
            currMusic.singer=[cell.musicInfo objectForKey:@"singer"];
            
            currMusic.singerIcon=[cell.musicInfo objectForKey:@"singericon"];
            
            currMusic.djID=[cell.musicInfo objectForKey:@"ID"];
            
            currMusic.musicID=[cell.musicInfo objectForKey:@"MusicID"];
            
            currMusic.playing = YES;
            
            currMusic.shareURL= [cell.musicInfo objectForKey:@"shareURL"];
            
            [ZYMusicTool setPlayingMusic:currMusic];
            
            [self addViewRes:currMusic];
            
        }
        [self musicPlay].playMode=@"one";
        
        [self.view.window.rootViewController presentViewController:[self musicPlay] animated:true completion:nil];
    }
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([tableView isEqual:self.djTableView])
    {
        NSString  *cellIdentifier = @"CityDJCell";
        CityDJCell *cell = (CityDJCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
            cell = [Objects objectAtIndex:0];
        }
        cell.isBanding1=NO;
         cell.isBanding2=NO;
         cell.isBanding3=NO;
        cell.selectionStyle= UITableViewCellSelectionStyleNone;
        cell.row= indexPath.row;
        cell.delegate=self;
        if(cityDjArray.count>0 && indexPath.row<2)
        {
            
            NSInteger i=indexPath.row*3;
            if(i<cityDjArray.count)
            {
                cell.isBanding1=YES;
                NSDictionary *dict1= [cityDjArray objectAtIndex:i];
                if([[dict1 objectForKey:@"path"] isEqualToString:@""])
                {
                    cell.img1.image= [UIImage imageNamed:@"2.jpg"];
                }
                else
                {
                    [Common downLoadImageByURL:cell.img1 andURL:[dict1 objectForKey:@"path"] andName:[dict1 objectForKey:@"pathname"]];
                }
                cell.lblMsg1.text=[dict1 objectForKey:@"Profile"];
                cell.lblName1.text=[dict1 objectForKey:@"Title"];
                cell.djID1=[dict1 objectForKey:@"id"];
            }
            
            i=indexPath.row*3+1;
            if(i<cityDjArray.count)
            {
                cell.isBanding2=YES;
                NSDictionary *dict2= [cityDjArray objectAtIndex:i];
                if([[dict2 objectForKey:@"path"] isEqualToString:@""])
                {
                    cell.img2.image= [UIImage imageNamed:@"2.jpg"];
                }
                else
                {
                    [Common downLoadImageByURL:cell.img2 andURL:[dict2 objectForKey:@"path"] andName:[dict2 objectForKey:@"pathname"]];
                }
                cell.lblMsg2.text=[dict2 objectForKey:@"Profile"];
                cell.lblName2.text=[dict2 objectForKey:@"Title"];
                cell.djID2=[dict2 objectForKey:@"id"];
            }
            
            i=indexPath.row*3+2;
            if(i<cityDjArray.count)
            {
                cell.isBanding3=YES;
                NSDictionary *dict3= [cityDjArray objectAtIndex:i];
                if([[dict3 objectForKey:@"path"] isEqualToString:@""])
                {
                    cell.img3.image= [UIImage imageNamed:@"2.jpg"];
                }
                else
                {
                    [Common downLoadImageByURL:cell.img3 andURL:[dict3 objectForKey:@"path"] andName:[dict3 objectForKey:@"pathname"]];
                }
                cell.lblMsg3.text=[dict3 objectForKey:@"Profile"];
                cell.lblName3.text=[dict3 objectForKey:@"Title"];
                cell.djID3=[dict3 objectForKey:@"id"];
            }
        }
        return cell;
    }
    else
    {
        
        
        if(musicArray.count>0 && indexPath.row < musicArray.count)
        {
            NSString  *cellIdentifier = @"MyTableCell";
            
            MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            
            if (cell == nil) {
                
                NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
                
                cell = [Objects objectAtIndex:0];
            }
            
            cell.selectionStyle= UITableViewCellSelectionStyleNone;
            
            
            NSInteger i=  indexPath.row;
            
            NSDictionary *musicInfo= [musicArray objectAtIndex:i];
            
            cell.musicInfo = musicInfo;
            
            cell.lblMusicAuthor.text = [musicInfo objectForKey:@"NickName"];
            
            cell.lblMusic.text = [musicInfo objectForKey:@"MusicName"];
            
            cell.musicID= [musicInfo objectForKey:@"MusicID"];
            
            NSString *iconName=[musicInfo objectForKey:@"IconName"];
            
            NSString *imgPath= [musicInfo objectForKey:@"Icon"];
            
            [Common downLoadImageByURL:cell.imgMusic andURL:imgPath andName:iconName];
            
            return cell;
        }
        else
        {
            
            
            NSString  *cellIdentifier = @"NewsMoreCell";
            
            MoreCell *cell = (MoreCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            
            if (cell == nil) {
                
                NSArray* Objects = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:tableView options:nil];
                
                cell = [Objects objectAtIndex:0];
                
            }
            
            cell.selectionStyle= UITableViewCellSelectionStyleNone;
            
            pageIndex=pageIndex+1;
            
            [self loadSameCityMusic] ;//加载下一页数据
            
            return cell;
        }
        
    }
}


@end
